/*
 * @Author: 徐建辰
 * @LastEditors: 徐建辰
 * @Description: 本地存储集中管理
 */

import {TOKEN, USER_ID, USER_INFO} from './constant'

// token
export const setToken = data => uni.setStorageSync(TOKEN, data)
export const getToken = () => uni.getStorageSync(TOKEN)
export const removeToken = () => uni.removeStorageSync(TOKEN)

// userId
export const setUserId = data => uni.setStorageSync(USER_INFO, data)
export const getUserId = () => uni.getStorageSync(USER_ID)

// userInfo
export const setUser = data => setStorage(USER_ID, data)
export const getUser = () => getStorage(USER_ID)
export const removeUser = () => uni.removeStorageSync(USER)

// remove操作
export const removeItem = key => uni.removeStorageSync(key)
export const removeAll = () => uni.clearStorageSync()

// 统一性的
export function setItem(key, value) {
  if (!key) {
    throw new Error('You must pass in a cached key')
  }
  if (typeof value === 'object') {
    value = JSON.stringify(value)
  }
  return uni.setStorageSync(key, value)
}
export function getItem(key) {
  const data = uni.getStorageSync(key)
  try {
    return JSON.parse(data)
  } catch (error) {
    return data
  }
}
